********Table J.1 - Mexico********

eststo clear

*Load data and create differences
cd "$reploc/data/created_data"
use mex_full, clear

sort pid_link mxfls
drop if mxfls==.
keep if panel==1

keep rbuc* mxfls pid_link inflation imp_age panel birthyr grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? birthstate *UB* *LB* yearprov

*Compute Hausdorff metric between intervals, using CDF normal distribution over R as value at each point

local xvars income_pc_monthly NarrowBracket  

foreach x in `xvars' {

gen `x'_midInt=.
replace `x'_midInt=(`x'_LB_A+`x'_UB_A)/2 if `x'_LB_A!=-9999999 & `x'_UB_A!=9999999
sum `x'_midInt
local muse=r(mean)
local sduse=r(sd)

gen `x'_UB_cdf=normal((`x'_UB_A-`muse')/`sduse')
replace `x'_UB_cdf=1 if `x'_UB_A==9999999
gen `x'_LB_cdf=normal((`x'_LB_A-`muse')/`sduse')
replace `x'_LB_cdf=0 if `x'_UB_A==-9999999

gen `x'_distUB=abs(`x'_UB_cdf-`x'_UB_cdf[_n-1]) if mxfls==3
gen `x'_distLB=abs(`x'_LB_cdf-`x'_LB_cdf[_n-1]) if mxfls==3
egen `x'_distmax=rowmax(`x'_distUB `x'_distLB) if mxfls==3

gen `x'_flagmax=0
replace `x'_flagmax=1 if `x'_distUB>=`x'_distLB
replace `x'_flagmax=2 if `x'_distUB<`x'_distLB

gen `x'_distuse=.
replace `x'_distuse=`x'_distmax if mxfls==3 & `x'_flagmax==1 & `x'_UB_cdf>=`x'_UB_cdf[_n-1]
replace `x'_distuse=-`x'_distmax if mxfls==3 & `x'_flagmax==1 & `x'_UB_cdf<`x'_UB_cdf[_n-1]
replace `x'_distuse=`x'_distmax if mxfls==3 & `x'_flagmax==2 & `x'_LB_cdf>=`x'_LB_cdf[_n-1]
replace `x'_distuse=-`x'_distmax if mxfls==3 & `x'_flagmax==2 & `x'_LB_cdf<`x'_LB_cdf[_n-1]
replace `x'_distuse=0 if mxfls==2 & `x'_distuse[_n+1]!=.
}

drop *flagmax* *distmax* *midInt*

*Compute elements to use as mid-point
foreach x in `xvars' {

gen `x'_midIntuse=.
replace `x'_midIntuse=(`x'_LB_A+`x'_UB_A)/2 if `x'_LB_A!=-9999999 & `x'_UB_A!=9999999

gen `x'_distublbmid=`x'_midIntuse-`x'_LB_A if `x'_LB_A!=-9999999 & `x'_UB_A!=9999999
sum `x'_distublbmid, d
local avgdist=r(mean)

replace `x'_midIntuse=`x'_LB_A+`avgdist' if `x'_LB_A!=-9999999 & `x'_UB_A==9999999
replace `x'_midIntuse=`x'_UB_A-`avgdist' if `x'_LB_A==-9999999 & `x'_UB_A!=9999999

}

drop *distublbmid*

reshape wide rbuc*  inflation imp_age panel grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? *_distuse *_midIntuse *UB* *LB*, i(pid_link) j(mxfls)


local varlist1 rbuc rbuc_nga inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 income_pc_monthly_midIntuse NarrowBracket_midIntuse income_pc_monthly_distuse NarrowBracket_distuse


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'3-`var'2
}

*Generate cohort by birthyear variables to cluster on
gen coh_prov_clust=yearprov

*Do these results for primary sample
gen primarysample1=0
replace primarysample1=1 if rbuc_ngadif!=. & grbrprovAvg_lam0co3dif!=. & grbrprovSD_lam0co3dif!=. & inflationdif!=. &  birthyr>=1925 & coh_prov_clust!=.

egen primarysample=total(primarysample1), by(pid_link)

keep if primarysample==1

*Regressions
local depvar income_pc_monthly_midIntuse NarrowBracket_midIntuse income_pc_monthly_distuse NarrowBracket_distuse
local rhsvari oldmexonly
local rhsvaruse _lam0co3dif
local usei grbr

foreach x in `depvar' {

preserve

drop if `x'dif==.

eststo:  reg `x'dif  `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse'  inflationdif, vce(cluster coh_prov_clust)

restore

}

cd "$reploc/results"	
esttab using TableJ1_Mexico.tex, star(* 0.1 ** 0.05 *** 0.01) b(2) se(2) replace
